package com.java.kids.controller;

import com.java.kids.Service.PatientService;
import com.java.kids.model.Appointment;
import com.java.kids.model.Patient;
import com.java.kids.model.patientState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class PatientController {

    @Autowired
    private PatientService patientService;

    //登录验证
    @GetMapping("/patient/login")
    public Patient login(@RequestParam("username") String username,@RequestParam("password") String password){
        Patient patient=patientService.getPatient(username,password);
        if (patient!=null){
            return patient;
        }
        return null;
    }
    //验证该用户是否存在
    @GetMapping("/patient/exist")
    public int isExist(@RequestParam("username") String username){
        if (patientService.getPatientByUsername(username)==null){
            return 1;
        }
        return -1;
    }
    //注册
    @PostMapping("/patient/register")
    public int register(@RequestParam("username") String username,@RequestParam("password") String password,
                        @RequestParam("pName")String pName,@RequestParam("gender")Integer pgender,
                        @RequestParam("birthday")String pbirthday,@RequestParam("phone")String pphone,
                        @RequestParam("more") String pmore
                        ){
        Patient patient=new Patient();
        patient.MyPatient(username,password,pName,pbirthday,pgender, pphone,pmore);
        return patientService.insertPatient(patient);
    }
    //找到一个病患的信息(注册时判断名字是否重复)
    @GetMapping("/patient")
    public patientState getPatient(@RequestParam("username") String username){
        System.out.println(username+"---------------");
        Patient patient=patientService.getPatientByUsername(username);
        if (patient!=null){
            return new patientState(1,patient.pusername,patient.pphone);
        }
         return new patientState(2,patient.pusername,patient.pphone);//可以
    }

    //修改用户信息
    @PostMapping("/patient/update")
    public Patient updatePatient(@RequestBody Patient patient){
        System.out.println(patient);
        if (patient.getPusername()!=""){
            if (patientService.updatePatient(patient)==1){
                return patientService.getPatientByUsername(patient.getPusername());
            }
        }
        return null;
    }

    /*
    邮箱发送测试
    url: http://localhost:8085/mail
     */
    @Autowired
    JavaMailSenderImpl mailSender;
    @Value("${spring.mail.username}")
    String from = "1617196273@qq.com";
    String to = "1617196273@qq.com";
    String subject = "hello";
    String text = "你好";
    @GetMapping("mail")
    public String sendMail() {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(text);
            mailSender.send(message);
            return "发送成功";
        } catch (MailException e) {
            e.printStackTrace();
            return "发送失败";
        }
    }
}
